<?php

class RoleController extends Silk_Controller_Admin_Action {

    protected $_norefer = array(
	'create', 'edit', 'delete'
    );


    public function init(){
	parent::init();
	$this->appendBreadcrumbs(Silk::Translate('Manage Roles'), $this->resolveRoute('role_admin'));
    }

    public function adminAction(){

	$this->initPaginatorParams('silk_cms_roles___', 'name');

	$session = Silk::getSession();

	if ($this->_request->has('criteria'))
	    $session->criteria = $this->_getParam('criteria');
	else
	    $session->criteria = null;

	$Roles = new Silk_Roles();

	$paginator = $Roles->getPaginator($session->criteria, $session->offset, $session->limit, "{$session->sort} {$session->order}");

	$paginator->setItemCountPerPage($session->limit);
	$paginator->setCurrentPageNumber($session->page);

	$this->view->roles = $paginator;

    }

    public function createAction(){
	$this->appendBreadcrumbs(Silk::Translate('Create Role'));
	$Roles = new Silk_Roles();

	if ($this->_request->isPost()){

	    if (strtolower($this->_getParam('submit')) == 'cancel') $this->gotoRoute('role_admin');

	    try {

		$role = $Roles->create($this->_getParam('role'));
		$role->save();
		$this->gotoRoute('role_admin');

	    } catch (FP_Validation_Exception $x){

		foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
	    }

	} else
	    $role = $Roles->create();

	$this->view->role = $role;
    }

    public function editAction(){
	$this->appendBreadcrumbs(Silk::Translate('Edit Role'));
	$Roles = new Silk_Roles();
	$role  = $Roles->fetch($this->_getParam('role_id'));

	if ($role){
	    if ($this->_request->isPost()){

		if (strtolower($this->_getParam('submit')) == 'cancel') $this->gotoRoute('role_admin');

		try {

		    $role->import($this->_getParam('role'));
		    $role->save();
		    $this->gotoRoute('role_admin');

		} catch (FP_Validation_Exception $x){

		    foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
		}

	    }

	    $this->view->role = $role;
	} else
	    throw new FP_Route_Exception('Invalid role id', FP_Route_Exception::NO_RESOURCE);
    }

    public function deleteAction(){
	$this->appendBreadcrumbs(Silk::Translate('Delete Role'));
	$Roles = new Silk_Roles();
	$role  = $Roles->fetch($this->_getParam('role_id'));

	if ($role){
	    if ($this->_request->isPost()){

		if (strtolower($this->_getParam('submit')) == 'cancel') $this->gotoRoute('role_admin');

		try {

		    $role->delete();
		    $this->gotoRoute('role_admin');

		} catch (Silk_Validation_Exception $x){

		    foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
		}

	    }

	    $this->view->role = $role;
	} else
	    throw new FP_Route_Exception('Invalid role id', FP_Route_Exception::NO_RESOURCE);
    }

}

?>